package com.manong.dao;

import com.manong.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Dorr
 * @since 2025-03-03
 */
public interface RoleMapper extends BaseMapper<Role> {
    /**
     * 检查角色是否已分配给用户
     * @param id
     * @return int
     */
    @Select("select count(*) from sys_user_role where role_id=#{id}")
    int findRoleCountByRoleId(Long id);

    /**
     * 删除角色和权限的关系
     * @param id
     */
    @Delete("delete from sys_role_permission where role_id = #{id}")
    void deleteRolePermissionByRoleId(Long id);

    /**
     * 添加角色和权限的关系
     * @param roleId
     * @param permissionIds
     * @return
     */
    int saveRolePermission(Long roleId, List<Long> permissionIds);

    /**
     * 根据用户ID查询该用户拥有的角色ID
     * @param userId
     * @return
     */
    @Select("select role_id from sys_user_role where user_id = #{userId}")
    List<Long> findRoleIdByUserId(Long userId);
}

